package com.chinawayltd.ews.sample.util.grid;

import com.chinawayltd.ews.sample.util.StringUtils;
import com.chinawayltd.ews.sample.util.excel.ExportCloumsModel;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.List;

@ApiModel
public class Page {
    public Page() {
    }

    public Page(int pageNo, int pageSize, String sortStr, int sort) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.sortStr = sortStr;
        this.sort = sort;
        initSortParam();
    }

    @ApiModelProperty(value="当前页码" ,required=true)
    private Integer pageNo;
    @ApiModelProperty(value="当页最大显示行数，如果pageSize等于-1就代表不需要进行分页操作" ,required=true)
    private Integer pageSize;
    @ApiModelProperty(value="列表排序的标识",example="id desc")
    private String sortStr;
    @ApiModelProperty(hidden = true)
    private Integer count;
    @ApiModelProperty(hidden = true)
    private String sortCloumn;
    @ApiModelProperty(hidden = true)
    private String sortType;
    @ApiModelProperty(value="需要导出到excel显示的列,导出时必填")
    private List<ExportCloumsModel> cloums;
    @ApiModelProperty(hidden = true)
    private Integer sort = 1;//用来直接拼字符串排序，1为按   this.sortCloumn和this.sortType 排序，0为按传来的sortStr字符串排序
    public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public String getSortStr() {
        return sortStr;
    }

    public void setSortStr(String sortStr) {
        this.sortStr = sortStr;
    }

    public List<ExportCloumsModel> getCloums() {
        return cloums;
    }

    public void setCloums(List<ExportCloumsModel> cloums) {
        this.cloums = cloums;
    }

    public String getSortCloumn() {
        if(StringUtils.isBlank(this.sortCloumn) && StringUtils.isNotBlank(this.sortStr)){
            this.initSortParam();
        }
        return sortCloumn;
    }

    public String getSortType() {
        if(StringUtils.isBlank(this.sortType) && StringUtils.isNotBlank(this.sortStr)){
            this.initSortParam();
        }
        return sortType;
    }

    protected void initSortParam() {
    	if(StringUtils.isNotBlank(this.sortStr)){
    		if(this.sort!=1){
    			this.sortCloumn =sortStr;
    			this.sortType = "";
    		}else{
    			String[] sortStrSplit = sortStr.split(" ");
                if(sortStrSplit.length == 2){
                    this.sortCloumn = StringUtils.camelToUnderline(sortStrSplit[0]);
                    this.sortType = sortStrSplit[1];
                }
    		}
        }
    }

}
